package com.sakiko._02_springai_chatclient.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * projectName: com.sakiko._02_springai_chatclient.controller
 *
 * @author: Yang
 * description: chat-client controller
 * time: 2025/8/7 22:19
 */
@RestController
@RequestMapping("chatclient")
public class ChatController {

    // @autowired 这个找不到 chatClient 要使用 ChatClient.Builder build()一个实例
    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    // 1、实现简单对话功能
    @GetMapping(value = "chat", produces = "text/html;charset=UTF-8")
    public String chat(@RequestParam(value = "msg", defaultValue = "会话没有传递信息参数，返回no即可") String message) {

        return chatClient.prompt() // 提示词
                .user(message) // 用户输入信息
                .call() // 请求模型
                .content(); // 文本

    }
}
